package com.yxx.sys.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxx.data.common.DataConstast;
import com.yxx.data.domain.Wound;
import com.yxx.data.service.WoundService;
import com.yxx.sys.common.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;

/**
 * @Author: YuXinXin
 * @Description:
 * @Date:Created in 10:30 2022/5/17
 **/
@RestController
@RequestMapping("/file")
@Api("文件控制器")
public class FileController {
  @Autowired
  WoundService woundService;

  @GetMapping("/loadCMImg")
  @ApiOperation("创面图片下载")
  public ResponseEntity<Object> loadCMImg(String path){
      return AppFileUtils.createResponseEntity(path, DataConstast.CMImg);
  }

  @GetMapping("/loadCMZip")
  @ApiOperation("创面信息zip包下载")
  public ResponseEntity<Object> loadCMZip(String path){
    return AppFileUtils.createResponseEntity(path,DataConstast.CMZip);
  }

  @GetMapping("/loadCMLabel")
  @ApiOperation("创面标注下载")
  public ResponseEntity<Object> loadCMLabel(String path){
    return AppFileUtils.createResponseEntity(path, DataConstast.CMLabel);
  }


  @PostMapping("/uploadCMImg")
  @ApiOperation("创面图片上传")
  public DataGridView uploadCMImg(MultipartFile mf,String oldPicturePath) {
    //修改图片 而非初次上传 就删除原图
    String oldPictureName=null;
    if(!StrUtil.equals(oldPicturePath,"/defaultImage.jpg"))
    {
      AppFileUtils.removeFile(FileUtil.file(AppFileUtils.UPLOAD_CM_PATH+oldPicturePath));
    }
    String path = AppFileUtils.uploadFile(mf,AppFileUtils.UPLOAD_CM_PATH);
    Map<String,Object> map=new HashMap<String, Object>();
    map.put("path",path);
    return new DataGridView(map);
  }

  @PostMapping("/uploadCMLabel")
  @ApiOperation("标注图片上传")
  public DataGridView uploadCMLabel(MultipartFile mf,String oldPicturePath) {
    //修改图片 而非初次上传 就删除原图
    String oldPictureName=null;
    if(!StrUtil.equals(oldPicturePath,"/defaultImage.png"))
    {
      AppFileUtils.removeFile(FileUtil.file(AppFileUtils.UPLOAD_CM_LABEL_PATH+oldPicturePath));
    }
    String path = AppFileUtils.uploadFile(mf,AppFileUtils.UPLOAD_CM_LABEL_PATH);
    Map<String,Object> map=new HashMap<String, Object>();
    map.put("path",path);
    return new DataGridView(map);
  }

  @GetMapping("/deleteUselessImg")
  @ApiOperation("当年的无用创面图片删除")
  public Result deleteUselessImg() {
    String year= DateUtil.format(new Date(), "yyyy");
    String yearPath = AppFileUtils.UPLOAD_CM_PATH + "/" + year;
    File[] monthDayDirectories = FileUtil.ls(yearPath);
    int imgDeleteIdx=0;
    int imgIdx=0;
    for (File monthDayDirectory : monthDayDirectories) {
      File[] imgs = FileUtil.ls(monthDayDirectory.getAbsolutePath());
      for (File img : imgs) {
        QueryWrapper<Wound> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("picture_path",AppFileUtils.getRelativePath(img.getAbsolutePath()));
        int count = woundService.count(queryWrapper);
        if(count==0){
          AppFileUtils.removeFile(img);
          imgDeleteIdx++;
        }
        imgIdx++;
      }
    }
    return new Result(200,"图片总数："+imgIdx+"删除图片数："+imgDeleteIdx);
  }
}
